package biz.datalk.industrialland.admin.modules.online.config.util;

/**
 *
 *
 * @author tarofang@163.com
 * @date 2020-01-06
 *
 */
public class DynfromEnhanceJsUtil {

    public static String getJsStr(String jsFuncStr) {
        return "class DatalkDynfromEnhanceJs{constructor(){} " + jsFuncStr + " }";
    }

    /** 将双引号 转成 \" */
    public static String doubleQuoteEscape(String str) {
        if (str == null) {
            return null;
        }
        int len = str.length();
        if (len <= 1) {
            return str;
        }

        StringBuffer sb = new StringBuffer();
        for(int idx = 0; idx < len; idx ++) {
            char c = str.charAt(idx);
            if (c == '\n' || c == '\t' || c=='\r') {
                continue;
            }

            if (c == '"') {
                if (idx > 0 && str.charAt(idx - 1) == '\\') {
                    sb.append(c);
                } else {
                    sb.append('\\').append(c);
                }
                continue;
            }
            sb.append(c);
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        try {
            String s = "\\\\\"aaa\\\"bbb\"ccc'ddxd";
            s = "\"aaaaaaaaa\"sdlj\\\\\"fl\"\\xxxx\"''\"sk'jdf";
            System.out.println(s);

            // System.out.println(HtmlUtils.htmlEscape(s));
            System.out.println(doubleQuoteEscape(s));
            // System.out.println(s.replaceAll("\"", "\\\\\""));

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
